//
//  billViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/13.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "billViewController.h"
#import "billTableViewCell.h"

@interface billViewController ()<UITableViewDelegate , UITableViewDataSource>{
    
    
}

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,strong) UITableView *billTableView;
@property(nonatomic,strong) UILabel *moneyNumLabel;

@end

@implementation billViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
}

#pragma mark ----------------------- requestData ---------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: @"1" forKey:@"page"];
    
    [[MNetworkHandler sharedInstance]conURL:more_finance
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       _dataArray = returnData[@"data"];
                                       NSLog(@"\n\n\n===账单===\n\n\n%@",_dataArray);
                                       [_billTableView reloadData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    self.title = @"我的账单";
    
    _billTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64)];
    _billTableView.backgroundColor = getColor(@"ffffff");
    _billTableView.delegate = self;
    _billTableView.dataSource = self;
    _billTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_billTableView];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return screenScale(60);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[billTableViewCell class] forCellReuseIdentifier:@"identifier"];
    billTableViewCell *cell = (billTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.nameLable.text = _dataArray[indexPath.row][@"type"];
    cell.dateLable.text = _dataArray[indexPath.row][@"add_time"];
    cell.messageLable.text = _dataArray[indexPath.row][@"content"];
    
    if ([_dataArray[indexPath.row][@"money_type"] isEqualToString:@"1"]) {
        
        cell.moneyNumLable.text = _dataArray[indexPath.row][@"money"];
        
    }else{
    
        cell.moneyNumLable.text = [NSString stringWithFormat:@"-%@", _dataArray[indexPath.row][@"money"]];
        
    }
    
    return cell;
}

@end
